package net.enclosing.basecamp;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;

public class Project {
	String id = "";
	String name = "";
	String activeStatus = "";
	public static void main(String[] args) {
		testConnection();
	}
	public static Collection<Project> recents() throws ParseException{
		List<Milestone> milestones = Milestone.recents();
//		ArrayList<Project> recents = new ArrayList<Project>();
		HashMap<Integer, Project> recents = new HashMap<Integer, Project>();
		for (Milestone milestone : milestones) {
			Project p = milestone.getProject();
			if(recents.get(new Integer(milestone.getProject().getId())) == null){
				recents.put(new Integer(p.getId()),p);
			}
		}
		return recents.values();
	}

	public static Project create(int projectid) {
		Document document = Basecamp.get("projects/"+projectid+".xml");
		Element projectElement = document.getRootElement();
		Project project = new Project();
		project.setId(projectElement.elementText("id"));
		project.setActiveStatus(projectElement.elementText("status"));
		project.setName(projectElement.elementText("name"));
		return project;
	}
	public static List<Project> actives(){
		String path = "projects.xml";
		LocalStaticData staticData = new LocalStaticData();
		Document  document =  Basecamp.get(path);
		Element projectsElement = document.getRootElement();
		List<Element> projectElements = projectsElement.elements();
		ArrayList<Project> projects = new ArrayList<Project>();
		for (Element projectElement : projectElements) {
			Project project = new Project();
			if(projectElement.element("status").getText().equals("active")){
				project.setId(projectElement.element("id").getText());
				project.setName(projectElement.element("name").getText());
				projects.add(project);
			}
		}
		return projects;
	}
	public static void testConnection(){
		
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getActiveStatus() {
		return activeStatus;
	}
	public void setActiveStatus(String activeStatus) {
		this.activeStatus = activeStatus;
	}
	
}
